import network
import utime as time
import modem


#联网状态标志
g_connect_status = False

def on_4g_cb(args):
    global g_connect_status
    pdp = args[0]
    netwk_sta = args[1]
    if netwk_sta == 1:
        g_connect_status = True
    else:
        g_connect_status = False

def connect_network():
    global Net,on_4g_cb,g_connect_status
    Net = network.NetWorkClient()
    g_register_network = False
    if Net._stagecode is not None and Net._stagecode == 3 and Net._subcode == 1:
        g_register_network = True
    else:
        g_register_network = False
    if g_register_network:
        Net.on(1,on_4g_cb)
        Net.connect(None)
    else:
        print('network register failed')
        modem.reboot()      #上电后联网失败自动重启
        return False
    while True:
        if g_connect_status:
            print('network register successed')
            return True
        time.sleep_ms(20)

#联网状态标志位
def getNetFlag():
    global g_connect_status
    return  g_connect_status   

